package pingji.controller.upload;

import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import pingji.service.upload.UploadService;

import java.io.IOException;
import java.io.InputStream;

@Controller
@CrossOrigin(origins = "*")
@Log
public class UploadController {
    @Autowired
    UploadService service;
    @RequestMapping("/upload")
    public String upload(@RequestParam("image")MultipartFile file,
                         @RequestParam("auth_requirement") String desc,
                        @RequestParam("id") Integer id,
                         @RequestParam("select_pingji") Integer pingjierId) throws IOException {
        if(id==null){
            return "redirect:/html/pingji_login.html";
        }
        log.info(file.toString());
        log.info(desc);
        log.info(id.toString());
        service.upload(file, desc, id,pingjierId);
        return "redirect:/html/pingji_order.html?id="+id;
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam("id")Integer id,@RequestParam("userId") Integer userId){
        if(userId==null){
            return "redirect:/html/pingji_login.html";
        }
        if(id==null){
            return "redirect:/html/pingji_order.html?id="+userId;
        }
        service.delete(id,userId);
        return "redirect:/html/pingji_order.html?id="+userId;
    }
    @RequestMapping("/shensu")
    public String shensu(@RequestParam ("id")Integer id,@RequestParam("userId")Integer userId ){
        if(userId==null){
            return "redirect:/html/pingji_login.html";
        }
        if(id==null){
            return "redirect:/html/pingji_order.html?id="+userId;
        }
        service.shensu(id,userId);
        return "redirect:/html/pingji_order.html?id="+userId;
    }
}
